package com.gxa.ehome.proxy;

import com.gxa.ehome.service.SysUserService;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

@Component("invocationHandler")
public class SysUserServiceInvocationHandler implements InvocationHandler {

//    通过jdk动态代理实现日志自动记录
    // 企业中对于日志类型的要求: 业务日志/操作日志/error日志

    private SysUserService sysUserService = null;

    public SysUserServiceInvocationHandler(SysUserService sysUserService){
        this.sysUserService = sysUserService;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 操作前的记录: 执行的方法:methodName,传递的参数是:[1,2,3]
        System.out.println("执行的方法:"+method.getName()+",传递的参数是:"+ Arrays.toString(args));
        Object invoke = method.invoke(sysUserService, args);
        //操作后的记录
        System.out.println("方法成功执行,返回的结果是:"+invoke);
        return invoke;

        // 如果使用异常增强, 可参考如下格式
       /* try{
            //before
            Object invoke = method.invoke(sysUserService, args);
            // afterReturning
        }catch(Exception e){
            // afterThrowing
        }finally{
            // after
        }*/
    }
}
